/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common.statistics;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author risto
 */
public class Series extends ArrayList<Double> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7250002517948147021L;
	
	
	/**
	 * Series data
	 */
//    private List<Double> data;

    /**
     * Add number
     * @param d
     */
//    public void add(double d) { data.add(d); }
 //   public void add(int i) { data.add((double)i); }

	public void add(int i) { add((double) i); }
	
    public Series() {
   //     data = new ArrayList<Double>();
    }

    /**
     * Merge data from series s to this.
     * @param s
     */
    public void merge(Series s) {
    	addAll(s);
    }
    
    /**
     * Clear contents
     */
/*    public void clear()
    {
    	data.clear();
    }
  */  
    /**
     * Return series values.
     * @return
     */
    public double[] values() {
    	double[] ret = new double[size()];
    	for (int i = 0; i < size(); i++) {
    		ret[i] = get(i);
    	}
    	return ret;
    }
    
    /**
     * Sum of data in series
     * @return
     */
    public double sum()
    {
    	double sum = 0;
    	for (Double d : this) sum += d.doubleValue();
    	return sum;
    }

    /**
     * Sample mean (i.e. average)
     * @return
     */
    public double mean()
    {
    	double N = size();
        return (1.0 / N) * sum();
    }
    
    /**
     * Compute Sum[ (x - mean)^2 ]
     * @return
     */
    private double sqMeanDiff()
    {
    	double dsum = 0, mean = mean();
        for (double d : this) dsum += Math.pow(d - mean, 2);
        return dsum;
    }
    
    /**
     * Variance
     * @return
     */
    public double var()
    {
    	double N = size();
        double var = sqMeanDiff() / N;
        return var;
    }
    
    /**
     * Standard deviation of sample, 1/N * sum(x-mean)^2. Maximum-likelihood estimate but biased for small n.
     * @return
     */
    public double stdevp()
    {
    	double N = size();
        return Math.sqrt(sqMeanDiff() / N);
    }

    /**
     * Sample standard deviation of sample, 1/(N-1) * sum(x-mean)^2.
     * @return
     */
    public double stdev()
    {
    	double N = size();
        return Math.sqrt(sqMeanDiff() / (N - 1));
    }
    
    /**
     * number of data points
     * @return
     */
    public int count()
    {
    	return size();
    }

    /**
     * Return mean and deviation
     * @return
     */
    public double[] meanAndDev()
    {
    	return new double[] { mean(), stdev() };
    }
    
   
    public String formatNormDist()
    {
    	return String.format("%.5f±%.5f", mean(), stdev());
    }
    
    /**
     * error/distance to series b.
     * @param b
     * @return
     */
    public double error(Series b) {
    	return error(b.mean(), b.stdev());
    }
    
    public double error(double mean, double stdev) {
    	double dm = mean() - mean;
    	double dsd = stdev() - stdev;
    	return Math.sqrt(dm*dm+dsd*dsd);
    }

}
